package com.googlecode.perfectable.persistable;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public final class DeletingFileVisitor extends SimpleFileVisitor<Path> {
	private static final DeletingFileVisitor INSTANCE = new DeletingFileVisitor();

	private DeletingFileVisitor() {
		// prevents external initialization
	}

	@Override
	public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
		Files.delete(file);
		return FileVisitResult.CONTINUE;
	}

	@Override
	public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
		Files.delete(dir);
		return FileVisitResult.CONTINUE;
	}

	public static void deleteRecursivly(Path base) throws IOException {
		Files.walkFileTree(base, DeletingFileVisitor.INSTANCE);
	}
}
